October 08, 2017

Questions

Overview

  • Reproducibility & why it's important
  • rmarkdown and knitr

Reproducibility

Reproducibility & Replicability

Reproducibility – the ability to recompute results – and replicability – the chances other experimenters will achieve a consistent result – are two foundational characteristics of successful scientific research.

Source

Why is it important?

Analytical Workflow

Reproducibility IRL

The Sad Analyst's Infinite Loop:

  1. Data preparation
  2. Modeling
  3. Generating report
  4. Something wrong in your data
  5. Repeat 1 ~ 4

Traditional Approach

Better Approach

R Markdown – Our Better Approach

rmarkdown and knitr

Installing

Run the following to install the necessary packages:

install.packages(c('rmarkdown', 'knitr'))

R Markdown – Our Better Approach

Looks a lot like:

What is Markdown?

What is Markdown?

Markdown is a lightweight markup language, originally created by John Gruber and Aaron Swartz allowing people "to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML)".

What is Markdown?

Markdown Preview Tools

Markdown Quick Reference in RStudio

Markdown Basics

Emphasis

*italic*   **bold**
_italic_   __bold__

italic
bold

Headers

# Header 1

## Header 2

### Header 3

Manual Line Breaks

End a line with two or more spaces:

Roses are red,  
Violets are blue.

Lists

Unordered List:

* Item 1
* Item 2
    + Item 2a
    - Item 2b

Ordered List:

1. Item 1
2. Item 2
3. Item 3
    + Item 3a
    + Item 3b

Unordered List:

  • Item 1
  • Item 2
    • Item 2a
    • Item 2b

Ordered List:

  1. Item 1
  2. Item 2
  3. Item 3
    • Item 3a
    • Item 3b

Links

Images

Inline Image:

![my-img](../images/betr-appr2.png)

Reference Image:

![my-img](ref)

[ref]: ../images/betr-appr2.png

Blockquotes

Blockquotes let you use the <blockquote> tag to let readers know you are quoting someone:

> Statistical thinking will one day be as necessary a qualification for efficient
> citizenship as the ability to read and write. --H.G. Wells

Statistical thinking will one day be as necessary a qualification for efficient citizenship as the ability to read and write. –H.G. Wells

Plain Code Blocks

Plain code blocks are displayed in a fixed-width font but not evaulated.

 ```
This text is displayed verbatim / preformatted  
Useful if you want to inlcude code from other languages  
Or just want rigid formatting
 ```

Embedding Equations

LaTeX Inline equation: $equation$

Display equation: $$ equation $$

$$ 
    \large{ \sigma^2 = \frac { \sum_{i=1}^{n} \left( x_i - \bar{x} \right) ^ 2} {n-1} }
$$

\[ \large{ \sigma^2 = \frac { \sum_{i=1}^{n} \left( x_i - \bar{x} \right) ^ 2} {n-1} }\]

Horizontal Rules

You can use either of the following to insert a horizontal rule (<hr> tag) in your document to split up different sections.

---------

*********

Tables

First Header Second Header
Content Cell Content Cell
Content Cell Content Cell
First Header  | Second Header
------------- | -------------
Content Cell  | Content Cell
Content Cell  | Content Cell

Integrating R Code

R Code Chunks

Output Options

Tables (A more useful example…)

library(knitr) # install if necessary
my_table <- data.frame(Col1 = letters[1:3], Col2 = 1:3)
kable(my_table)
Col1 Col2
a 1
b 2
c 3

Anatomy of an R Markdown Document

Our Example Document

YAML Metadata: YAML document options

  • Title and author info
  • Defines document output format
    • we will focus on HTML
    • can be multiple
  • Specifies additional output options
  • Can include links to CSS files here

Markdown: Article text

  • Markdown is used for the text of your document
  • Use any of the syntax shown in class
  • Complete reference here

R Code Chunk: Executible R code

  • Any R code is allowed
  • Will be executed sequentially to generate output
  • Create graphs, numerical output, tables

Creating an R Markdown Document in RStudio

Compiling an R Markdown Document

R Markdown Live Preview (Experimental)

Install editR from here

Sharing Your Work

rmarkdown resources

Lab/Homework